package semant.first_pass;

import semant.Env;
import symbol.ClassInfo;
import symbol.Symbol;
import symbol.VarInfo;
import syntaxtree.VarDecl;
import syntaxtree.VisitorAdapter;

/**
 * 
 * @author gabriel
 * 
 */
public class AtributeHandler extends VisitorAdapter {
	private Env env;
	private ClassInfo info;

	private AtributeHandler(Env e, ClassInfo i) {
		env = e;
		info = i;
	}

	public static void firstPass(Env e, ClassInfo i, VarDecl node) {
		AtributeHandler h = new AtributeHandler(e, i);
		node.accept(h);
	}

	@Override
	public void visit(VarDecl node) {
		Symbol name = Symbol.symbol(node.name.s);
		VarInfo v = new VarInfo(node.type, name);

		// insere o atributo na tabela de simbolos
		if (!info.addAttribute(v)) {
			VarInfo old = info.attributes.get(name);

			env.err.Error(node.name, new Object[] {
					"Redeclaracao do atributo \'" + name + "\' "
							+ "no classe: \'" + info.name + "\'. ",
					"Declaracaoo anterior em: [" + old.type.line + ", "
							+ old.type.row + "]" });
		}

	}

}
